home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1082.dms / q1082.adf / src.lzh / Fig / latex_line.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  3KB  |  155 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : Aug 1985.
  7.  *
  8.  *    %W%    %G%
  9. */
  10. #include "math.h"
  11.  
  12. int  pgcd(a,b)
  13.     int a, b;
  14. /*
  15.  *  compute greatest common divisor, assuming 0 < a <= b
  16.  */
  17. {
  18.     b = b % a;
  19.     return (b)? gcd(b, a): a;
  20. }
  21.  
  22. int  gcd(a, b)
  23.     int a, b;
  24. /*
  25.  *  compute greatest common divisor
  26.  */
  27. {
  28.     if (a < 0) a = -a;
  29.     if (b < 0) b = -b;
  30.     return (a <= b)? pgcd(a, b): pgcd(b, a);
  31. }
  32.  
  33.  
  34. int  lcm(a, b)
  35.     int a, b;
  36. /*
  37.  *  compute least common multiple
  38.  */
  39. {
  40.     return abs(a*b)/gcd(a,b);
  41. }
  42.  
  43.  
  44. double rad2deg = 57.295779513082320877;
  45.  
  46. struct angle_table {
  47.     int    x, y;
  48.     double angle;
  49. };
  50.  
  51. struct angle_table line_angles[25] =
  52.   { {0, 1, 90.0}, 
  53.     {1, 0,  0.0},
  54.     {1, 1, 45.0}, 
  55.     {1, 2, 63.434948822922010648},
  56.     {1, 3, 71.565051177077989351},
  57.     {1, 4, 75.963756532073521417},
  58.     {1, 5, 78.690067525979786913},
  59.     {1, 6, 80.537677791974382609},
  60.     {2, 1, 26.565051177077989351},
  61.     {2, 3, 56.309932474020213086},
  62.     {2, 5, 68.198590513648188229}, 
  63.     {3, 1, 18.434948822922010648},
  64.     {3, 2, 33.690067525979786913},
  65.     {3, 4, 53.130102354155978703},
  66.     {3, 5, 59.036243467926478582},
  67.     {4, 1, 14.036243467926478588},
  68.     {4, 3, 36.869897645844021297},
  69.     {4, 5, 51.340191745909909396},
  70.     {5, 1, 11.309932474020213086},
  71.     {5, 2, 21.801409486351811770},
  72.     {5, 3, 30.963756532073521417},
  73.     {5, 4, 38.659808254090090604},
  74.     {5, 6, 50.194428907734805993},
  75.     {6, 1, 9.4623222080256173906},
  76.     {6, 5, 39.805571092265194006}
  77.   };
  78.  
  79. struct angle_table arrow_angles[13] =
  80.   { {0, 1, 90.0}, 
  81.     {1, 0,  0.0},
  82.     {1, 1, 45.0}, 
  83.     {1, 2, 63.434948822922010648},
  84.     {1, 3, 71.565051177077989351},
  85.     {1, 4, 75.963756532073521417},
  86.     {2, 1, 26.565051177077989351},
  87.     {2, 3, 56.309932474020213086},
  88.     {3, 1, 18.434948822922010648},
  89.     {3, 2, 33.690067525979786913},
  90.     {3, 4, 53.130102354155978703},
  91.     {4, 1, 14.036243467926478588},
  92.     {4, 3, 36.869897645844021297},
  93.   };
  94.  
  95. get_slope(dx, dy, sxp, syp, arrow)
  96.     int  dx, dy, *sxp, *syp, arrow;
  97. {
  98.     double angle;
  99.     int    i, s, max;
  100.     double d, d1;
  101.     struct angle_table *st;
  102.  
  103.     if (dx == 0) {
  104.     *sxp = 0;
  105.     *syp = (dy < 0)? -1: 1;
  106.     return;
  107.     }
  108.     angle = atan((double)abs(dy) / (double)abs(dx)) * rad2deg;
  109.     if (arrow) {
  110.     st = arrow_angles;
  111.     max = 13;
  112.     } else {
  113.     st = line_angles;
  114.     max = 25;
  115.     }
  116.     s = 0;
  117.     d = 9.9e9;
  118.     for (i = 0; i < max; i++) {
  119.     d1 = fabs(angle - st[i].angle);
  120.     if (d1 < d) {
  121.         s = i;
  122.         d = d1;
  123.     } 
  124.     }
  125.     *sxp = st[s].x;
  126.     if (dx < 0) *sxp = -*sxp;
  127.     *syp = st[s].y;
  128.     if (dy < 0) *syp = -*syp;
  129. }
  130.  
  131. latex_endpoint(x1, y1, x2, y2, xout, yout, arrow, magnet)
  132.     int  x1, y1, x2, y2;
  133.     int  *xout, *yout;
  134.     int  arrow, magnet;
  135. {
  136.     int    dx, dy, sx, sy, ds, dsx, dsy;
  137.  
  138.     dx = x2-x1;
  139.     dy = y2-y1;
  140.     get_slope(dx, dy, &sx, &sy, arrow);
  141.     if (abs(sx) >= abs(sy)) {
  142.     ds = lcm(sx, magnet*gcd(sx,magnet));
  143.     dsx = (2*abs(dx)/ds + 1)/2;
  144.     dsx = (dx >= 0)? dsx*ds: -dsx*ds;
  145.     *xout = x1 + dsx;
  146.     *yout = y1 + dsx*sy/sx;
  147.     } else {
  148.     ds = lcm(sy, magnet*gcd(sy,magnet));
  149.     dsy = (2*abs(dy)/ds + 1)/2;
  150.     dsy = (dy >= 0)? dsy*ds: -dsy*ds;
  151.     *yout = y1 + dsy;
  152.     *xout = x1 + dsy*sx/sy;
  153.     }
  154. }
  155.